<?php
/**
 * 菜单管理
 * @author Zwc
 * Created On 2021/2/19 21:14
 */

namespace app\auth\controller;


use app\auth\service\MenuService;
use app\common\controller\AdminBaseController;
use think\facade\View;

class Menu extends AdminBaseController
{
    /**
     * 列表
     */
    public function index() {
        return View::fetch('index');
    }

    /**
     * 获取数据
     */
    public function getData() {
        $params = $this->request->get();
        $service = new MenuService();
        $data = $service->getData($params);
        return ['code' => 0,'msg' => '获取数据成功','data' => $data];
    }

    /**
     * 添加
     */
    public function add() {
        if($this->request->isPost()) {
            $params = $this->request->post();
            $service = new MenuService();
            return $service->add($params);
        }else {
            $service = new MenuService();
            $data = $service->getAllMenu();
            return View::fetch('add',[
                'menus' => $data,
            ]);
        }
    }

    /**
     * 编辑
     */
    public function edit($id) {
        $service = new MenuService();
        if($this->request->isPost()) {
            $params = $this->request->post();
            $service = new MenuService();
            return $service->edit($params);
        }else {
            $detail = $service->detail($id);
            $menus = $service->getData([]);
            return View::fetch('edit',[
                'data' => $detail,
                'menus' => $menus,
            ]);
        }
    }

    /**
     * 删除
     */
    public function del($id) {
        $service = new MenuService();
        $params = [
            'del_flag' => 1
        ];
        return $service->modify(['id' => $id],$params);
    }

    /**
     * 获取菜单
     */
    public function menus() {
        $service = new MenuService();
        $datas = $service->getMenuTree();
        return ['code' => 0,'data' => $datas];
    }


}